package com.liangdemo.web.test;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Mr.Liang
 * @date 2022/4/28 9:27
 */

public class TextController {
    public static List<User> users = new ArrayList<>();

    static {
        users.add(new User(1, "JACK", 22));
        users.add(new User(2, "LILY", 21));
        users.add(new User(3, "LUCY", 24));
        users.add(new User(4, "TOM", 23));
        users.add(new User(5, "TOMAS", 25));
    }

    public static void main(String[] args) {
        // 题目一、将users集合中的元素按照年龄 age 从大到小的顺序排列
        // 1、使用比较器完整写法：
        users.sort(new Comparator<User>() {
            @Override
            public int compare(User o1, User o2) {
                return o2.getAge() - o1.getAge();
            }
        });
        System.out.println("users = " + users);

        // 2、简化版
        users.sort((o1, o2) -> o2.getAge() - o1.getAge());
        System.out.println("users = " + users);



        // 题目二、将users集合中所有元素的 id 取出，拼接成逗号分割的字符串，如：1,2,3,4,5
        // 思路一：遍历取出id，手动拼接
        StringBuilder stringBuilder = new StringBuilder();
        for (User user : users) {
            stringBuilder.append(user.getId()).append(",");
        }
        String ids = stringBuilder.substring(0, stringBuilder.length() - 1);
        System.out.println("ids = " + ids);

        // 思路二：遍历取出id形成新的集合，使用String.join()方法拼接；
        List<String> idList = new ArrayList<>();
        for (User user : users) {
            idList.add(user.getId().toString());
        }
        ids = String.join(",", idList);
        System.out.println("ids = " + ids);

        // 思路三：使用stream流
       ids = users.stream().map(user -> user.getId().toString()).collect(Collectors.joining(","));
        System.out.println("ids = " + ids);


        // 题目三、统计字符串 str 中字母 I 出现的次数
        String str = "ILOVECHINA";
        // 思路一：分割字符串遍历，逐个统计
        int times = 0;
        String[] split = str.split("");
        for (String s : split) {
            if("I".equals(s)) {
                times++;
            }
        }
        System.out.println("times = " + times);

        // 思路二：使用String.indexOf()方法查找
        // 定义起始位置
        int index = 0;
        int times1 = 0;
        while (str.indexOf("I", index) != -1) {
            times1++;
            index = str.indexOf("I", index) + 1;
        }
        System.out.println("times1 = " + times1);
    }
}

class User{

    private Integer id;

    private String name;

    private Integer age;

    public User() {
    }

    public User(Integer id, String name, Integer age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
